﻿using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace Infrastructure.Controls
{
    public static class BindEntityToControls<T> where T: class,new()
    {
        /// <summary>
        /// Binds an entity list to a dropdownlist.
        /// </summary>
        /// <param name="ddl">The dropdownlist.</param>
        /// <param name="entityList">The entity list.</param>
        /// <param name="IdPropertyNameText">The property name for text.</param>
        /// <param name="IdPropertyNameValue">The property name for value.</param>
        /// <param name="TextForAll">The text for ALL.</param>
        public static void BindEntityListToDDL(DropDownList ddl, IList<T> entityList,
                                               string IdPropertyNameText,string IdPropertyNameValue, string TextForAll)
        {
            ddl.Items.Clear();
            foreach (var item in entityList)
            {
                string propertyForText = item.GetType().GetProperty(IdPropertyNameText).GetValue(item,null).ToString();
                string propertyForValue =item.GetType().GetProperty(IdPropertyNameValue).GetValue(item, null).ToString();

                ddl.Items.Add(new ListItem(propertyForText, propertyForValue));
            }

            if (TextForAll !=null)
                ddl.Items.Insert(0,TextForAll);
        }

    }
}